package com.lynx.system.controller;

import com.lynx.system.beans.Menu;
import com.lynx.system.beans.Type;
import com.lynx.system.feign.MenuFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuFeign menuFeign;

    @GetMapping("redirect/{location}")
    public String redirect(@PathVariable("location") String location){
        return location;
    }

    @GetMapping("/findAllMenu")
    @ResponseBody
    public Object findAll(@RequestParam("page") int index, @RequestParam("limit") int limit){
        return menuFeign.findAll(index, limit);
    }


    @GetMapping("/deleteById")
    public Object deleteById(int id){
        System.out.println("id==============="+id);
        menuFeign.deleteById(id);
        return "redirect:redirect/menu_manage";
    }
    @GetMapping("/toAdd")
    public ModelAndView findTypes(){
        ModelAndView modelAndView = new ModelAndView();
        List<Type> listType =  menuFeign.findTyps();
        modelAndView.addObject("list",listType);
        modelAndView.setViewName("menu_add");
        return modelAndView;
    }
    @PostMapping("/save")
    public Object addMenu(Menu menu){
        menuFeign.save(menu);
        return "redirect:redirect/menu_manage";
    }
    @GetMapping("/toEdit")
    public ModelAndView toEdit(int id){
        ModelAndView modelAndView = new ModelAndView();
        Menu menu = menuFeign.findById(id);
        List<Type> listType =  menuFeign.findTyps();
        modelAndView.addObject("menu",menu);
        modelAndView.addObject("list",listType);
        modelAndView.setViewName("menu_update");
        return modelAndView;
    }
    @PostMapping("/update")
    public Object update(Menu menu){
        menuFeign.edit(menu);
        return "redirect:redirect/menu_manage";
    }



}
